import { _decorator, Component, Node, Prefab, instantiate, find } from 'cc';
const { ccclass, property } = _decorator;

import { Consts } from "../Data/Consts";
import { bdlMgr } from "./SS_BundleManager";

@ccclass('SS_UIManager')
export default class SS_UIManager extends Component {
    static Instance: SS_UIManager;

    @property(Node)
    UI: Node | null = null;

    onLoad() {
        SS_UIManager.Instance = this;
    }

    ResetUINode() {
        SS_UIManager.Instance.UI = find("Canvas/UI");
    }

    async CreatePanel(_panelName: string, _params: any) {
        const Prefab_Grid = await bdlMgr.getRes<Prefab>(Consts.BundleName.PREFAB, _panelName, Prefab);
        const node: Node = instantiate(Prefab_Grid);
        const panel = node.getComponent(_params);

        if (!this.UI) {
            this.UI = find("Canvas/UI");
        }

        this.UI.addChild(node);
        panel?.ShowOn();
        return panel;
    }
}

/** 调试 */
window["SS_UIManager"] = SS_UIManager;